
"ui";
importClass(org.opencv.imgcodecs.Imgcodecs);//导入opencv的包
const tool = require('./tool.js');
const car = require('./car.js');
const xian = require('./xian.js');
ui.layout(
    <vertical h="4000">
        <horizontal>
            <Switch id="isOpenConsole" text="控制台" gravity="center" w="100" h="20" />
            <Switch id="debugMode" text="调试模式" gravity="center" w="130" h="20" />
        </horizontal>
        {/* <horizontal visibility="gone" > */}
        <horizontal>
            <text text="卡密" />
            <input id="keygen" w="200" text="" />
            <button id="saveParam" text="保存" />
        </horizontal>
        <horizontal>
            <button id="xian" text="仙侠神域" />
        </horizontal>
    </vertical>
);


//仙侠神域
ui.xian.click(function () {
    threads.start(function () {
        // if (!car.checkCar()) {
        //     return
        // }
        let params = collectParameters()
        tool.init(params)
        xian.main(params)
    });
});


//保存
ui.saveParam.click(function () {
    confirm("确认要保存吗?")
        .then(clear => {
            if (clear) {
                let p = car.updateCardInfo(collectParameters())
            }
        });
})


//收集参数
function collectParameters () {
    var params = car.readConfig()
    //卡密
    params.keygen = ui.keygen.text()
    //控制台开关
    params.isOpenConsole = ui.isOpenConsole.checked;
    //调试模式开关
    params.debugMode = ui.debugMode.checked;
    return params
}


//初始化配置数据
function initParameters () {
    try {
        //读取配置文件
        let params = car.readConfig()

        //卡密
        if (params.keygen !== "" && params.keygen !== undefined && params.keygen !== null) {
            ui.keygen.text(params.keygen)
        }

        // 控制台
        if (params.isOpenConsole !== "" && params.isOpenConsole !== undefined && params.isOpenConsole !== null) {
            ui.isOpenConsole.setChecked(params.isOpenConsole)
        }
        //调试模式
        if (params.debugMode !== "" && params.debugMode !== undefined && params.debugMode !== null) {
            ui.debugMode.setChecked(params.debugMode)
        }

        return params
    } catch (e) {
        console.log("读取参数异常:" + e)
    }
}

//初始化配置数据
function initParameters () {
    try {
        //读取配置文件
        let params = car.readConfig()

        //卡密
        if (params.keygen !== "" && params.keygen !== undefined && params.keygen !== null) {
            ui.keygen.text(params.keygen)
        }

        // 控制台
        if (params.isOpenConsole !== "" && params.isOpenConsole !== undefined && params.isOpenConsole !== null) {
            ui.isOpenConsole.setChecked(params.isOpenConsole)
        }
        //调试模式
        if (params.debugMode !== "" && params.debugMode !== undefined && params.debugMode !== null) {
            ui.debugMode.setChecked(params.debugMode)
        }

        return params
    } catch (e) {
        console.log("读取参数异常:" + e)
    }
}

initParameters()